<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ctManagementResource.ascx.cs" Inherits="KhoaLuan.MainSys.Manager.Controls.ctManagementResource" %>

<link href="../../../Style/StyleCommon.css" rel="stylesheet" />
<link href="../../../Style/modal.css" rel="stylesheet" />
<script src="../../../Scripts/AjaxUpload.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        LoadTabletbResource();
    });
    $(function () {
        var dialog, form,
     ResourceID = $("#txtResourceID"),
     ResourceName = $("#txtResourceName"),
     Path = $("#fileUploadResource"),
     Image = $("#txtImage"),
     Active = $("#txtActive"),
     Description = $("#txtDescription"),
     CreatedDate = $("#txtCreatedDate"),
     TypeResourcecID = $("#dllTypeResourcecIDEd"),
     OrgID = $("#dllOrgIDEd"),
       allFields = $([]).add(ResourceID).add(ResourceName).add(Path).add(Image).add(Active).add(Description).add(CreatedDate).add(TypeResourcecID).add(OrgID);

        $("#<%=btnThem.ClientID%>").click(function () {
            $('#<%=txtResourceID.ClientID %>').val("0");
            $('#<%=txtResourceName.ClientID %>').val("");
            $('#<%=fileUploadResource.ClientID %>').val("");
            $('#<%=txtImage.ClientID %>').val("");
            $('#<%=txtActive.ClientID %>').val("");
            $('#<%=txtDescription.ClientID %>').val("");
            $('#<%=txtCreatedDate.ClientID %>').val("11/11/2014");
            $('#<%=btnCapNhat.ClientID%>').hide();
            $('#<%=btnThemMoi.ClientID%>').show();
            overlay('1');
            false;
        });
        $("#<%=btnThemMoi.ClientID%>").click(function () {
            var path = $("#fileUploadResource").val();
            if (path == "") {
                alert("Vui long nhap duong dan file");
                return;
            }

            var name = $('#<%=txtResourceName.ClientID %>').val();
            if (name.trim() == "") {
                alert("Vui lòng nhập Name");
                return;
            }
            var image = $('#<%=txtImage.ClientID %>').val();
            var active = $('#<%=txtActive.ClientID %>').val();
            var description = $('#<%=txtDescription.ClientID %>').val();
            var date = $('#<%=txtCreatedDate.ClientID %>').val();
            var path = $('#<%=fileUploadResource.ClientID %>').val();
            var typeResource = $('#<%=dllTypeResourcecIDEd.ClientID %>').val();
            var orgID = $('#<%=dllOrgIDEd.ClientID%>').val();
            debugger;
            UploadAjax($("#<%=fileUploadResource.ClientID%>"), "/ProcessAJAX/UploadFileAJAX.ashx", function (response) {
                debugger;
                var obj = JSON.parse(response);
                if (obj.Status) {
                    var path = obj.Location;
                    $.ajax({
                        async: false,
                        type: 'GET',
                        url: "/MainSys/Manager/Resource/ResourceHandler.ashx",
                        data: {
                            method: 'Insert_tbResource',
                            args: {
                                ResourceName: name, Path: path, Image: image, Active: active, Description: description, TypeResourcecID: typeResource, OrgID: orgID
                            }
                        },
                        success: function (data) {
                            $('#<%=txtResourceName.ClientID %>').val("");
                            overlay(0);
                            var responseDel = JSON.parse(data);
                            if (responseDel.Status == "Success") {
                                EventPageClick(1, 5, null);
                            } else {
                                alert("Thêm  Resource thất bại");
                            }
                        }
                    });
                    }
            });
        });
        $("#<%=btnCapNhat.ClientID%>").click(function () {
            debugger;
            var id = $('#<%=txtResourceID.ClientID %>').val();
            var name = $('#<%=txtResourceName.ClientID %>').val();
            var path = $('#<%=fileUploadResource.ClientID %>').val();
            var image = $('#<%=txtImage.ClientID %>').val();
            var active = $('#<%=txtActive.ClientID %>').val();
            var description = $('#<%=txtDescription.ClientID %>').val();
            var date = $('#<%=txtCreatedDate.ClientID %>').val();
            var typeResource = $('#<%=dllTypeResourcecIDEd.ClientID %>').val();
            var orgID = $('#<%=dllOrgIDEd.ClientID%>').val();

            if (name.trim() == "") {
                alert("Vui lòng nhập Name");
                return;
            }
            $.ajax({
                async: false,
                type: 'GET',
                url: "/MainSys/Manager/Resource/ResourceHandler.ashx",
                data: {
                    method: 'UpdatetbResource',
                    args: {
                        ResourceID: id, CreatedDate: date, ResourceName: name, Path: path, Image: image, Active: active, Description: description, TypeResourcecID: typeResource, OrgID: orgID
                    }
                },
                success: function (data) {
                    $('#<%=txtResourceName.ClientID %>').val("");
                    overlay(0);
                    var responseDel = JSON.parse(data);
                    if (responseDel.Status == "Success") {
                        EventPageClick(1, 5, null);
                    } else {
                        alert("Update Group thất bại");
                    }
                }
            });
        });
    });
        function overlay(show) {
            el = document.getElementById("overlay");
            if (show == '1') {
                el.style.visibility = "visible";
                document.getElementById("rowID").style.visibility = "hidden";
            }
            else
                el.style.visibility = "hidden";
        }
        function EdittbResource(
        ResourceID,
        ResourceName,
        Path,
        Image,
        Active,
        Description,
        CreatedDate,
        TypeResourcecID,
        OrgID) {
            $('#<%=txtResourceID.ClientID%>').val(ResourceID);
            $('#<%=txtResourceName.ClientID%>').val(ResourceName);
            $('#<%=fileUploadResource.ClientID%>').val(Path);
            $('#<%=txtImage.ClientID%>').val(Image);
            $('#<%=txtActive.ClientID%>').val(Active);
            $('#<%=txtDescription.ClientID%>').val(Description);
            $('#<%=txtCreatedDate.ClientID%>').val(CreatedDate);
            $('#<%=dllTypeResourcecIDEd.ClientID %>').val(TypeResourcecID);
            $('#<%=dllOrgIDEd.ClientID %>').val(OrgID);
            $('#titleoverlay').text("Cap Nhat");
            $('#rowID').hide();
            $('#<%=btnThemMoi.ClientID%>').hide();
            $('#<%=btnCapNhat.ClientID%>').show();
            overlay('1');
        }
        function FillToDatable(listGroup) {
            debugger;
            var table = $("#tabletbResource > tbody").empty();
            listGroup.forEach(function (item) {
                var tr = $("<tr>");
                var td = $("<td>").append($('<input type="checkbox">'));
                tr.append(td);
                td = $("<td>").text(item.ResourceName);
                tr.append(td);
                td = $("<td>").text(item.Path);
                tr.append(td);
                td = $("<td>").text(item.Description);
                tr.append(td);
                td = $("<td>");
                var btnedit = $('<span style="cursor:pointer;">').text("Edit");
                debugger;
                btnedit.click(function () {
                    EdittbResource(item.ResourceID, item.ResourceName, item.Path, item.Image, item.Active, item.Description, item.CreatedDate, item.TypeResourcecID, item.OrgID);
                });
                td.append(btnedit);
                td.append("|");
                var btndel = $('<span style="cursor:pointer;">').attr("dataid", item.ResourceID).text("Delete");
                btndel.click(function () {
                    var id = $(this).attr("dataid");
                    var r = confirm("Do you want to remove this item");
                    var sender = $(this);
                    if (r == true) {
                        $.ajax({
                            async: false,
                            type: 'GET',
                            url: "/MainSys/Manager/Resource/ResourceHandler.ashx",
                            data: {
                                method: 'DeleteResource',
                                args: {
                                    ResourceID: parseInt(id)
                                }
                            },
                            success: function (data) {
                                var responseDel = JSON.parse(data);
                                if (responseDel.Status == "Success") {
                                    debugger;
                                    sender.parent().parent().remove();

                                } else {
                                    alert("Delete item not success");
                                }
                            }
                        });
                    } else {

                    }
                });
                td.append(btndel);
                tr.append(td);
                table.append(tr);
            });
        }

        function EventPageClick(intpage, intnumberinpage, txtkeyword) {
            debugger;
            $.ajax({

                async: false,
                type: 'GET',
                url: "/MainSys/Manager/Resource/ResourceHandler.ashx",
                data: {
                    method: 'GetListResource',
                    args: {
                        page: intpage,
                        keyword: txtkeyword,
                        numberinpage: intnumberinpage
                    }
                },
                success: function (data) {
                    var response = JSON.parse(data);
                    var listtbResource = JSON.parse(response.Data);
                    var table = $("#tabletbResource > tbody").empty();
                    FillToDatable(listtbResource);
                    CreatePaging(response.CurrentPage, intnumberinpage, response.TotalPage, txtkeyword);
                    debugger;
                }
            });
        }

        function CreatePaging(curentpage, numberinpage, totalpage, keyword) {
            debugger;
            var divpaging = $('#divpaging').empty();
            if (curentpage > totalpage)
                return;
            var i;
            var linkpage;
            for (i = curentpage - 3; i <= curentpage + 3; i++) {
                if (i > 0 && i <= totalpage) {
                    linkpage = $('<a href="javascript:void();">').text(i);
                    linkpage.attr('currentpage', i);
                    linkpage.attr('numberinpage', numberinpage);
                    linkpage.attr('keyword', keyword == null ? "" : keyword);
                    linkpage.click(function () {
                        debugger;
                        var curpage = $(this).attr('currentpage');
                        var numintpage = $(this).attr('numberinpage');
                        var keyword = $(this).attr('keyword');
                        EventPageClick(curpage, numintpage, keyword);
                    });
                    divpaging.append(linkpage).append(" - ");
                }
            }

        }

        function LoadTabletbResource() {
            debugger;
            EventPageClick(1, 5, null);
        }

        function LoadTabletbResourceTimkiem() {
            debugger

            var tam = $("#<%=txtTimKiem.ClientID%>").val();
            EventPageClick(1, 5, tam);

        }
        function Import() {
            debugger;
            var datafile = new FormData();
            var files = $("#fileUpload").get(0).files;
            if (files.length > 0) {
                datafile.append("File", files[0]);
            }
            datafile.append("TableName", "tbResource");
            $.ajax({
                type: "POST",
                url: "/ProcessAJAX/AJAXImport.ashx",
                contentType: false,
                processData: false,
                data: datafile,
                success: function (data) {
                    var res = JSON.parse(data);
                    if (res.Status == "Success") {
                        alert("import thanh cong.");
                        LoadTabletbResource();
                    }
                    else {
                        alert("import that bai.");
                    }
                }
            });

        }
</script>
<div class="jarviswidget jarviswidget-color-blueDark" id="wid-id-1" data-widget-editbutton="false" data-widget-fullscreenbutton="false">

    <header>
        <h2>Manage Resource	</h2>
        <div class="widget-toolbar">
            <div class="btn-group">
                <input class="btn btnthem dropdown-toggle btn-xs btn-success" type="button" id="btnThem" runat="server" style="margin-right: 10px;" value="Add new" />
                <input type="button" value="Show" class="btn btnthem dropdown-toggle btn-xs btn-success" onclick="LoadTabletbResource()" />
            </div>
        </div>
    </header>

    <div>
        <div class="widget-body widget-hide-overflow padding">
            <div class="col-xs-12 col-sm-6">
                <div id="datatable_tabletools_filter" class="dataTables_filter">
                    <label style="float: left">
                        <span class="input-group-addon"><i class="glyphicon glyphicon-search"></i></span>
                        <input type="search" class="form-control" placeholder="input keyword" id="txtTimKiem" runat="server" />
                        <input type="button" onclick="LoadTabletbResourceTimkiem();" value="Search" class="btn btn-default" style="width: 80px;" />
                    </label>
                </div>
            </div>
            <div class="col-sm-6 col-xs-6 hidden-xs" style="text-align: right;">
                <input type="file" id="fileUpload" accept="image/jpg" class="btn btn-default" />
                <input type="button" id="btnImport" runat="server" onclick="Import()" value="import" class="btn btn-default" />
            </div>
            <table id="tabletbResource" class="table table-bordered">
                <thead>
                    <tr>
                        <td></td>
                        <td>Resource Name
                        </td>
                        <td>Path
                        </td>
                        <td>Description
                        </td>
                    </tr>
                </thead>
                <tbody>
                </tbody>
            </table>
        </div>
        <div id="divpaging"></div>
    </div>
</div>

<div id="overlay">
    <div class="backgroundoverlay"></div>
    <div class="contentoverlay" style="width: 500px;">
        <div class="headeroverlay">
            <div style="float: right; cursor: pointer" onclick="overlay(0);">X</div>
            <div id="titleoverlay">Add new</div>
        </div>
        <div class="bodyoverlay">
            <fieldset>
                <table id="Table2">
                    <tr id="rowID">
                        <td>
                            <asp:Label ID="Label1" runat="server" Text="ResourceID"></asp:Label>
                        </td>
                        <td>
                            <asp:TextBox ID="txtResourceID" runat="server" Style="width: 100%;"></asp:TextBox>
                            <asp:Label ID="lblerror" runat="server" ForeColor="Red"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td>ResourceName </td>
                        <td>
                            <asp:TextBox ID="txtResourceName" runat="server" Visible="true" Width="200px" Style="width: 100%;"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>Path </td>
                        <td>
                            <asp:FileUpload ID="fileUploadResource" runat="server" />

                        </td>
                    </tr>
                    <tr>
                        <td>Image </td>
                        <td>
                            <asp:TextBox ID="txtImage" runat="server" Visible="true" Width="200px" Style="width: 100%;"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>Active </td>
                        <td>
                            <asp:TextBox ID="txtActive" runat="server" Visible="true" Width="200px" Style="width: 100%;"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>Description </td>
                        <td>
                            <asp:TextBox ID="txtDescription" runat="server" Visible="true" Width="200px" Style="width: 100%;"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>CreatedDate </td>
                        <td>
                            <asp:TextBox ID="txtCreatedDate" runat="server" Visible="true" Width="200px" Style="width: 100%;"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td style="width: 150px">TypeResourcecID</td>
                        <td style="width: 150px">
                            <asp:DropDownList ID="dllTypeResourcecIDEd" runat="server" Style="width: 100%;">
                            </asp:DropDownList></td>
                        <td>
                    </tr>
                    <tr>
                        <td style="width: 150px">TypeResourcecID</td>
                        <td style="width: 150px">
                            <asp:DropDownList ID="dllOrgIDEd" runat="server" Style="width: 100%;">
                            </asp:DropDownList></td>
                        <td>
                    </tr>
                </table>
                <input type="button" id="btnBack" class="them" onclick="overlay(0);" value="Back" />
                <input type="button" id="btnThemMoi" runat="server" class="them" onclick="overlay(0);" value="Add new" />
                <input type="button" id="btnCapNhat" runat="server" class="them" onclick="overlay(0);" value="Update" />
            </fieldset>
        </div>
    </div>
</div>

